var uploader;
$(function(){
	activeNav($("#sales-product"));
	//创建ueditor实例
	var ue = UE.getEditor('detail');
	
	uploader = WebUploader.create({
		auto : false,
		swf : ctx + 'static/plugins/webuploader/Uploader.swf',
		server : ctx + "/api/upload/file",
		pick: { id : '#upload-images-btn', multiple : true },
		fileVal : 'file',
		duplicate : true,
		method : 'POST',
		fileSingleSizeLimit : 2*1024*1000,
		accept : {
		    title : '选择图片(*.jpg,*.gif,*.png)',
		    extensions : 'gif,jpg,jpeg,png',
		    mimeTypes : 'image/*'
		},
		thumb : {width: 100,height: 100}
	});
	
	uploader.on( 'error', function( type ) {
		switch (type) {
			case 'Q_TYPE_DENIED':
				$.alert({message:'请选择正确的图片类型！'});
				break;
			case 'Q_EXCEED_SIZE_LIMIT':
				$.alert({message:'文件大小不能超过2MB！'});
				break;
			default:
				break;
		}
	});
	
	uploader.on( 'filesQueued', function( files  ) {
		$('.pick-btn').find('.webuploader-pick').text('继续添加');
		var li = $('.filelist li');
		if((li.length + files.length)>5){
			var cha = 5 - li.length;
			$(files).each(function(i,file){
				if(i<cha){
					uploader.makeThumb( file, function( error, src ) {
						generateImageHtml(src,false,file);
					});
				}else{
					uploader.removeFile( file );
				}
			});
			$.alert({message:'最多5张，多余的图片被移除！'});
		}else{
			$(files).each(function(i,file){
				uploader.makeThumb( file, function( error, src ) {
					generateImageHtml(src,false,file);
				});
			});
		}
	});
	
	uploader.on('uploadBeforeSend',function(object ,data , headers ){
		data.folder = "/productImage/";
	});
	
	uploader.on( 'startUpload', function() {
		$(".filelist li.queueNum").each(function(i){
			$(this).remove();
		});
	});
	
	uploader.on( 'uploadAccept', function( object,ret ) {
		generateImageHtml(ret.filePath,true);
	});
	
	uploader.on( 'uploadFinished', function() {
		formSubmit();
	});
	
	$('#save-btn').click(function(){
		uploader.upload();
	});
	
	//加载页面
	generateImageHtml($('#image1').val(),true);
	generateImageHtml($('#image2').val(),true);
	generateImageHtml($('#image3').val(),true);
	generateImageHtml($('#image4').val(),true);
	generateImageHtml($('#image5').val(),true);
	if($('#id').val()==''){
		$("#state1").addClass('active');
		$("#state1 input").attr("checked",true);
	}
	
});
function formSubmit(){
	$('.filelist input').each(function(i,obj){
		$(this).attr("name","image" + (i+1));
	});
	$('#form').submit();
}
function generateImageHtml(src,server,file){
	if(src != ''){
		var queueNumClass = 'queueNum';
		var imgSrc = src;
		if(server){
			imgSrc = ctx + src;
			queueNumClass = '';
		}
		var html = '<li class="' + queueNumClass + '"><p><img src="'+imgSrc+'" height="100" width="100">'
		+ '<div class="file-panel" style="height: 0px;">'
		+ '<input type="hidden" value="' + src + '"/>'
		+ '<span class="cancel">删除</span></div></p></li>';
		$(".filelist").append(html);
		$(".filelist li").on( 'mouseenter', function() {
		    $(this).find('.file-panel').stop().animate({height: 30});
		});
		$(".filelist li").on( 'mouseleave', function() {
			 $(this).find('.file-panel').animate({height: 0});
		});
		$(".filelist li").find('.cancel').click(function(){
			$(this).parents('li').remove();
			if(file != undefined){
				uploader.removeFile( file );
			}
		});
	}
}